package com.example.hotel_lxy.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.hotel_lxy.R;
import com.example.hotel_lxy.bean.StoreDateDialogBean;
import com.example.hotel_lxy.utils.DatePhotoUtils;
import com.example.hotel_lxy.utils.DateUtils;
import com.example.hotel_lxy.utils.DownloadUtils;
import com.ysxsoft.common_base.base.RBaseAdapter;
import com.ysxsoft.common_base.base.RViewHolder;
import com.ysxsoft.common_base.utils.DisplayUtils;
import com.ysxsoft.common_base.utils.ToastUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 *  日历（晚 午）
 * create by Sincerly on 9999/9/9 0009
 **/
public class StoreDateDialog extends Dialog {
    private Context mContext;
    private RecyclerView dialog_rv_view;
    private static String type;// 1 午  2 晚
    private static String data;// 1 午  2 晚
    private List<StoreDateDialogBean> mlist = new ArrayList<>();
    private String[] string = new String[]{"10:00", "10:30", "11:00", "11:30"
            , "12:00", "12:30", "13:00", "13:30"
            , "14:00", "14:30", "15:00", "15:30"};
    private String[] stringw = new String[]{"16:00", "16:30", "17:00", "17:30"
            , "18:00", "18:30", "19:00", "19:30"
            , "20:00", "20:30", "21:00", "21:30"
            , "22:00", "22:30", "23:00"};

    public StoreDateDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
      //  init();
    }

    private View init() {
        View view = View.inflate(mContext, R.layout.dialog_storedate, null);
        dialog_rv_view = view.findViewById(R.id.dialog_rv_view);
        showDataRv();
        return view;
    }

    private void showDataRv() {
        if (type.equals("1")) {
            for (int i = 0; i <string.length; i++) {
                StoreDateDialogBean storeDateDialogBean=new StoreDateDialogBean();
                storeDateDialogBean.setTime(string[i]);
                storeDateDialogBean.setaBoolean(false);
                mlist.add(storeDateDialogBean);
            }
        } else {
            for (int i = 0; i <stringw.length; i++) {
                StoreDateDialogBean storeDateDialogBean=new StoreDateDialogBean();
                storeDateDialogBean.setTime(stringw[i]);
                storeDateDialogBean.setaBoolean(false);
                mlist.add(storeDateDialogBean);
            }
        }
        dialog_rv_view.setLayoutManager(new GridLayoutManager(mContext, 3));
        RBaseAdapter<StoreDateDialogBean> storeDateDialogBeanRBaseAdapter=new RBaseAdapter<StoreDateDialogBean>(mContext,R.layout.item_dialog_storedate,mlist) {
            @Override
            protected void fillItem(RViewHolder holder, StoreDateDialogBean item, int position) {
                TextView text_time=holder.itemView.findViewById(R.id.text_time);
                text_time.setText(item.getTime());
                if(item.isaBoolean()){
                    text_time.setBackground(mContext.getResources().getDrawable(R.mipmap.icon_dialog_yes_bj));
                }else{
                    text_time.setBackground(mContext.getResources().getDrawable(R.mipmap.icon_dialog_no_bj));
                }
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        long timeGetTime =new Date().getTime();
                        String s= data + " "+item.getTime() + ":00";
                        long l = DatePhotoUtils.dateToStamp(s);
                            if(timeGetTime>l){
                                ToastUtils.shortToast(mContext,"到店时间不能早于当前时间");
                                return;
                           }
                        for (StoreDateDialogBean data : mlist) {
                            data.setaBoolean(false);
                        }
                        mlist.get(position).setaBoolean(true);
                        notifyDataSetChanged();
                        listener.onPositiveClick(item.getTime());
                        dismiss();
                    }
                });
            }

            @Override
            protected int getViewType(StoreDateDialogBean item, int position) {
                return 0;
            }
        };
        dialog_rv_view.setAdapter(storeDateDialogBeanRBaseAdapter);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCanceledOnTouchOutside(true);
//        setCancelable(false);
        setContentView(init());
    }

    public void showDialog() {
        if (!isShowing()) {
            show();
            WindowManager.LayoutParams lp = getWindow().getAttributes();
//            lp.height = DisplayUtils.getDisplayWidth(mContext) * 5 / 5;
            lp.width = DisplayUtils.getDisplayWidth(mContext) * 10 / 10;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            getWindow().setAttributes(lp);
            getWindow().setGravity(Gravity.BOTTOM);
        }
    }

    public static StoreDateDialog show(Context context, OnDialogClickListener listener, String types,String datas) {
        StoreDateDialog dialog = new StoreDateDialog(context, R.style.BottomDialogStyle);
        type = types;
        data=datas;
        dialog.setListener(listener);
        dialog.showDialog();
        return dialog;
    }

    public OnDialogClickListener listener;

    public StoreDateDialog setListener(OnDialogClickListener listener) {
        this.listener = listener;
        return this;
    }

    public interface OnDialogClickListener {
        /**
         * 点击确定按钮事件
         */
        public void onPositiveClick(String s);
    }
}